home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: uu4news.netcom.com!zodiac!szh
- From: szh@zcon.com (Syed Zaeem Hosain)
- Subject: Re: New C Programmer Has A Problem
- Message-ID: <1996Jan29.162514.3498@zcon.com>
- Sender: szh@zcon.com (Syed Zaeem Hosain)
- Nntp-Posting-Host: zodiac
- Reply-To: szh@zcon.com
- Organization: Z Consulting Group
- References: <4ehpa3$6kl@nntp.novia.net>
- Date: Mon, 29 Jan 1996 16:25:14 GMT
-
- In article <4ehpa3$6kl@nntp.novia.net>, tsyslo@oasis.novia.net (Tony Syslo) writes:
- > I have a question on C... why isn't this program working?!
-
- What are the detailed symptoms? Are they compilation or link or
- run-time errors?
-
- > Program START:
- > /* File Open */
- >
- > #include <clib/dos_protos.h>
- > #include <dos/dos.h>
- > #include <stdio.h>
- > #include <exec/types.h>
-
- The above includes are mostly non-standard (with the exception of
- stdio.h) as far as I can tell, so I am not sure if they are correct or
- not.
-
- > void main();
-
- This above declaration of what main returns is unnecessary and also
- wrong per ANSI requirements (see later correction).
-
- > char *name;
- > int age;
-
- Effectively, the above two variables have become global to the program.
- In this particular case, they could be inside the main function with
- similar results.
-
- > void main()
-
- Instead of the above, please use (see note earlier):
-
- int main()
-
- >
- > {
- > struct FileHandle *file_handle;
-
- I do not know what the above structure is. Why not just use the FILE
- pointer declaration as follows:
-
- FILE *file_handle;
-
- Then you can use the more easy fopen, fclose, fread, fwrite, fprintf,
- etc., functions to deal with data in the file.
-
- > long bytes_written;
- > long bytes_read;
- >
- > printf("Enter your name: ");
- > scanf("%s",name);
-
- This line above is one of the places where you should expect problems
- (there are others in your code, but this is one of the common errors).
-
- The comp.lang.c FAQ talks about this issue (allocating a pointer but
- not initializing it to point to any valid memory before you attempt to
- store data into that unassigned location).
-
- Specifically, section 7 (Memory Allocation) in the FAQ should be read
- thoroughly by you before proceeding.
-
- > printf("\nEnter your age: "); scanf("%d",age);
- >
- >
- > file_handle=(struct FileHandle *)
- > Open("RAM:You.dat",MODE_NEWFILE);
-
- As mentioned earlier, "fopen" is probably more easy to use. If declared
- as I had mentioned earlier, the above line would become:
-
- file_handle = fopen("RAM:You.dat","w");
-
- > /* Have we opened the file successfully? */
- > if(file_handle==NULL)
- > {
- > printf("Could not open the file!\n");
- > Exit(0);
-
- If main() is declared to be int return, then the above could be:
-
- return(0);
-
- although it is more common to return 0 to imply success, so it might
- be better to use:
-
- return(1);
-
- or something more appropriate if you want to trap the result of the
- return outside the program.
-
- > }
- > /* We have now opened a file, and are ready to start writing: */
- > bytes_written=Write(file_handle,name,sizeof(name));
-
- What is the "Write" function? It is not one of the standard library
- functions I am familiar with.
-
- > bytes_written=bytes_written+Write(file_handle,age,sizeof(age));
-
- Some simplification (using a technique in C that is quite common) will
- make the above line read as follows (even though it may be incorrect
- code since I am not aware of what "Write" does):
-
- bytes_written += Write(file_handle,age,sizeof(age));
-
- >
- > if(bytes_written!=sizeof(name)+sizeof(age))
- > {
- > printf("Could not save the list!\n");
- > Close(file_handle);
- > Exit(0);
- > }
- > else
- > printf("Saved successfully!\n");
- > printf("Memory cleared!\n");
- > name="";
-
- Oops. You may have difficulty with the above ... the pointer "name" will
- get set to point to another location other than allocated char array
- space (after you fix the other error).
-
- > age=0;
- > printf("Loading!\n");
- > Seek(file_handle,0,OFFSET_BEGINNING);
-
- Is "Seek" a valid library function?
-
- > bytes_read=Read(file_handle,name,sizeof(name));
-
- The function name "Read" above is unknown to me.
-
- > bytes_read=bytes_read+Read(file_handle,age,sizeof(age));
-
- Try (although see note above about the function "Read"):
-
- bytes_read += Read(file_handle,age,sizeof(age));
-
- > if(bytes_written!=sizeof(name)+sizeof(age))
- > {
- > printf("Could not read the list!\n");
- > Close(file_handle);
- > Exit(0);
- > }
-
- Your error handling (using the number of bytes read) for determiningg
- that the data was not read correctly is not a cleanly consistent method
- I would recommend, although it probably will work in this case.
-
- > /* Print out the data: */
- > printf("Hello, %s!\",name);
- ^
- Are you missing something here?
-
- > printf("You are %d years old!\n",age);
- > /* Close the file: */
- > Close(file_handle);
- > }
- >
- > Program END:
- >
- > Any help if MUCH appreciated...
- >
- > I am using the SAS/C 6.50 compiler...
-
- Well, in a number of places, your compiler should have complained about
- incorrect code, or incorrect library functions (possibly - I do not
- know the SAS compiler). Did this even compile and link correctly? If
- so, I would recommend switching compilers <smile>. Other wise, I would
- ask what symptoms you specifically experienced, and what you tried to
- fix, so that we can address how you should progress next.
-
- > Also, is there any of you whom might have a routine to figure up random (or
- > even psuedo-random) numbers?? Some algorithm, or such, as I need one BADLY!!
-
- Please look in the comp.lang.c FAQ for answers to the above question!
-
- Z
-
-
- --
- -------------------------------------------------------------------------
- | Syed Zaeem Hosain P. O. Box 610097 (408) 441-7021 |
- | Z Consulting Group San Jose, CA 95161 szh@zcon.com |
- -------------------------------------------------------------------------
-